/**
 * 新版文档管理模块
 * 
 * @Date: 2016-9-8
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(['jquery', 'init','fullcalendar','fullcalendar_zh-cn','moment','bsUtil','deptUserSelect',
			'bsButton','datagridServ','bsMsgServ','bsAddressListEdit'], 
	function($,init,fullcalendar,calendarLang,moment,bsUtil,deptUserSelect,
				bsButton,datagridServ,bsMsgServ,bsAddressListEdit) {

	'use strict';

	var groupQryUrl = '/groupCustom/getListDomain.action',	
		detailQryUrl='/groupCustomDetail/getListDomainByGroupid.action',	
		
		//模块代码定义		
		
		moduCode = {
			//通讯录
			groupDetail: 'groupCustomDetail',
			//分组管理
			group: 'groupCustom'
		},
		
		//当前模块
		currentModu,		
		
		//编辑页面url
		groupEditUrl='/groupCustom/query/getPage.action?code=bsGroupCustomDlg',
		detailEditUrl='/groupCustomDetail/query/getPage.action?code=bsGroupDetailDlg'
	//js参数
	var jsParam
	
	//群组名称树dom对象,群组成员明细列表对象
	var $groupgrid,$detailgrid
	
	//弹窗dom对象
	var $edit_window
	
	//选中群组对象
	var selectedGroup
	
	//选中群组明细
	var selectedDetail
	
	var subrootid = 'group'
	
	var rootid = 'bs-address-list';
	var private_method = {
		
		
		
		//初始化分组管理
		initGroup:function(){
			private_method.initTypeList();
			
			private_method.initBtn({bizcode:moduCode.group,btnType:'list',bizType:2});
			private_method.initBtn({bizcode:moduCode.groupDetail,btnType:'list',bizType:2});
			private_method.initDetailList();
			private_method.bind_btn_click();
		}		
		
		,
		/**
		 * 初始化科室列表
		 */
		initTypeList:function(){ 
			selectedGroup = null;
			var typeParam = {
				rownumbers:true,
				animate: true,									
				striped:true,
				border:true,
				singleSelect: true,
				idField: 'id',								
				fitColumns: true,
				url : '/' +init.getSite() + groupQryUrl,
				onClickRow:private_method.getDetailList,
				columns:[[
					{field:'ck',checkbox:true},	
					{field:'name',title:'群组名称',width:100,halign:'center'}
				]]
				
			}
			
			
			$groupgrid=$('#'+rootid + ' #' + subrootid +' .dg-type').datagrid(typeParam);
			
//			$typegrid.treegrid('options').url =  '/'+init.getSite() + deptQryUrl;
		}
		,
		/**
		 * 初始化明细列表
		 */
		initDetailList:function(){
			var _detailParam = {				
				url : '/'+init.getSite() + detailQryUrl,
				height:init.getGridHeight(),
				rownumbers:true,
				animate: true,									
				striped:true,
				border:true,
				//singleSelect:false,
				idField: 'id',									
				fitColumns: true,
				checkOnSelect:true,
				pagination : true,
				selectOnCheck:true,
				columns:[[
					{field:'ck',checkbox:true},
					{field:'deptName',title:'部门名称',width:50,halign:'center'} ,
					{field:'name',title:'用户姓名',width:30,halign:'center'}, 				
					{field:'position',title:'职位',width:30,halign:'center'} ,
					{field:'workphone',title:'工作电话',width:40,halign:'center'},
					{field:'mobile',title:'移动电话',width:40,halign:'center'},        
					{field:'email',title:'邮箱',width:50,halign:'center'},        
					{field:'qq',title:'QQ',width:40,halign:'center'} ,
					{field:'weixin',title:'微信',width:50,halign:'center'}									
				]]
			}
			
			
			$detailgrid = $('#'+rootid + ' #'+subrootid +' .dg-list').datagrid(_detailParam);
			
			
		},
		
		/**
		 * 
		 * @param _options.container
		 */
		initBtn:function(_options){
			if(window.console){
				console.log(' detail list button init ...')
			}
//			var _btn = bsButton.getBtn({bizcode:moduCode.addr,btnType:'list',bizType:2});
			var _btn = bsButton.getBtn(_options);
			if(_btn){
				var _c = $('#'+rootid + ' .btn-container');
				if(_c.length>0){
					_c.empty();
					_c.append(_btn);
				}
			}
			
		}
		,
		//获取文档明细列表
		getDetailList:function(index,rowData){
			selectedGroup = rowData;
			$detailgrid.datagrid('load',{typeId:rowData.id})
		}
		,
		bind_btn_click:function(e){
			var _btn = $('#'+rootid + ' button')
			if(_btn.length>0){
				_btn.unbind('click');
				_btn.bind('click',private_method.btn_listenner);
			}
		}
		,
		btn_listenner:function(e){
			init.applyBtnEvt({btnEvt:e,btnConfig:btnEvent});
		}
		
		,
		
		
		getCheckData:function(records){
			var _data,tid
			if(records.length==1){
				_data = {id:records[0].id};	
			}else{
				$.each(records,function(i,el){
					tid=!tid?el.id:tid+','+el.id
				})
				_data = {ids:tid}
			}
			
			return _data;
		}
		,
		/**
		 * @param _options.userId	选中用户id
		 * @param _options.userName 选中用户姓名
		 * @param _options.deptid	选中部门id
		 * @param _options.deptName 选中部门名称
		 * @param _options.data		选中人员集合
		 */
		addUserList:function(_options){
			if(selectedGroup && selectedGroup.id && _options && _options.userId){
				$.ajax({
					url:'/' +init.getSite() + '/groupCustomDetail/saveList.action',
					data:{typeId:selectedGroup.id,userId:_options.userId},
					type:'post',
					dataType:'json',
					success:function(data){
						$('#'+rootid + ' #'+subrootid +' .dg-list').datagrid('loadData',data);
						bsMsgServ.msgSlide('群组成员操作成功！');
						
						if(_options.evt){
							$(_options.evt.currentTarget).removeClass('disabled');
						}
					}
					,
					error:function(){
						bsMsgServ.errorSlide('群组成员操作失败！');
						if(_options.evt){
							$(_options.evt.currentTarget).removeClass('disabled');
						}
					}
				
				})
			}else{
				if(_options.evt){
					$(_options.evt.currentTarget).removeClass('disabled');
				}				
			}
		}
	}
	
	var btnEvent =  {
		listAddReplace:function(e){
			if(!selectedGroup){
				bsMsgServ.errorSlide('请选择群组名称');
				$(e.currentTarget).removeClass('disabled');
				return ;
			}		
			
			deptUserSelect.openPage({
				callback:private_method.addUserList,
				evt:e
			});
		}
		,
		listDel:function(e){
			
			var _data,tid
			
			var _grid = $('#'+rootid + ' #'+ subrootid + ' .dg-list')
			var records = _grid.datagrid('getSelections');
			if(records.length>0){
				
				
				if(records.length==1){
					_data = {id:selectedRecord.id};	
				}else{
					$.each(records,function(i,el){
						tid=!tid?el.id:tid+','+el.id
					})
					_data = {ids:tid}
				}
				
			}else{
				bsMsgServ.warnSlide('请选择要删除的记录');
				$(e.currentTarget).removeClass('disabled');
				return
			}
								
			$.messager.confirm('警告', '确定执行删除操作?', function(r){
						
				if( r){					
					$.ajax({
						url:'/'+init.getSite()+'/groupCustomDetail/del.action',
					 	contentType:'form',
					 	data:_data,
					 	success:function(){						 		
							_grid.datagrid('clearSelections');
							_grid.datagrid('reload');
							bsMsgServ.msgSlide('删除记录操作成功！');
							$(e.currentTarget).removeClass('disabled');
					 	}
					 	,
					 	failure:function(){
					 		
					 		bsMsgServ.errorSlide(
					 			codeDef.getConfig({
					 				type:'operate',code:'error'
					 			})
					 		);
					 		$(e.currentTarget).removeClass('disabled');
					 	}
					})
				}
			})
		}
		
	}
	
	var public_method = {
		
		//初始化函数
		initPage:function(){
			private_method.initGroup();
		}
		,
		//脚本打开页面
		openPage:function(_options){
			var _url = '/'+init.getSite()+'/common/query/getPage.action?code=userSelect';
			
			var _container = $('#user-select-pop')
			
			if(_container.length<=0){
				$('body').append('<div id="user-select-pop"></div>');
			}
			
			_container = $('#user-select-pop')
			_container.load(_url);
		}
	}
	
	
	return public_method;
});